﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.SatinAlma.YeniYapi;
using ConfExpert.Utility;

using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class POHistory : XForm
    {
        public POHistory()
        {
            InitializeComponent();
        }

        private void POHistory_Load(object sender, EventArgs e)
        {
            this.Text = "Satınalma Takip";
            

            string today = DateTime.Today.ToString("d");

            gvAksPOTalepLines.Init("GetAksPOSubmitted",0, -1, string.Empty, 0, 0, 0, 0, string.Empty,  string.Empty, 0,WhoAmI);
            gvAksPOTalepLines.AddColumn("KalanAdet", typeof(int), "ISNULL(QtyOrdered,QtyNeeded)-ISNULL(ToplamGelen,0)");
            gvAksPOTalepLines.AddColumn("TerminiGecmis", typeof(int), "Iif(Termin<'" + today + "' and Isnull(KalanAdet,0)>0 and stTalepId not in (4,9), 1, 0)");
            gvAksPOTalepLines.AddColumn("TedarikciTerminiGecmis", typeof(int), "Iif(TedarikciTermini <'" + today + "' and Isnull(KalanAdet,0)>0 and stTalepId not in (4,9), 1, 0)");

            gvAksPOTalepLines.FormatAsNumber(0, "QtyNeeded", "QtyOrdered", "ToplamGelen", "KalanAdet");
            gvAksPOTalepLines.FormatAsDate("SiparisTarihi", "TalepTarihi", "Termin", "TedarikciTermini");
            gvAksPOTalepLines.AddSummary("QtyNeeded", "QtyOrdered", "ToplamGelen", "KalanAdet");
            gvAksPOTalepLines.ShowFindPanel();

            gvAksPOTalepLines.YanYanaGetirSoluna("GelisTarihi", "KalanAdet");
            //gvAksPOTalepLines.MakeReadOnly( "QtyOrdered", "ToplamGelen", "VendorId", "AnlasilanFiyat");
            gvAksPOTalepLines.MakeColumnInvisible("AksPOId", "AksPOLinesId", "TerminiGecmis", "TedarikciTerminiGecmis","AksesuarId"); 
            gvAksPOTalepLines.SetCaptions("QtyNeeded:Talep Adedi", "QtyOrdered:Sipariş Adedi", "ToplamGelen:Gelen Adet", "VendorId:Tedarikci", "Notes:Notlar", "SipNo:TalepNo", "stTalepId:Durum");

            gvAksPOTalepLines.FormatAsNumber(3, "AnlasilanFiyat");
            //gvAksPOTalepLines.FormatAsNumber(1, "QtyOrdered");

            //gvAksPOTalepLines.YanYanaGetir("VendorId:NumuneVarMi");

            GridManager gm = GridManager.GetManager(gvAksPOTalepLines);
            gm.GridMenu.AddMenu("KAPAT", TalebiKapat);
            gm.GridMenu.AddMenu("İPTAL ET", CancelTalep);
            gm.GridMenu.AddMenu("Geri Al", UndoTalep);

            btnGetir.Click += new EventHandler(btnGetir_Click);
            btnTemizle.Click += new EventHandler(btnTemizle_Click);

            CreateVerticalGrid();

            emptyEditor = new RepositoryItemButtonEdit();
            emptyEditor.Buttons.Clear();
            emptyEditor.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
            gcAksPOTalepLines.RepositoryItems.Add(emptyEditor);

            gvAksPOTalepLines.FocusedRowChanged += gvAksPOTalepLines_FocusedRowChanged;
            gvAksPOTalepLines.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gvHist_RowCellStyle);
            gvAksPOTalepLines.RowStyle += gvAksPOTalepLines_RowStyle;
            gvAksPOTalepLines.CustomRowCellEdit += gvHist_CustomRowCellEdit;
            gvAksPOTalepLines.RowCellClick += gvHist_RowCellClick;
            bbiSave.ItemClick += bbiSave_ItemClick;
            bbiFoy.ItemClick += bbiFoy_ItemClick;
            gvAksPOTalepLines.CellValueChanged += gvHist_CellValueChanged;
            btnSipOlustur.ItemClick += btnSipOlustur_ItemClick;
            Yenile(); 
            gvAksPOTalepLines_FocusedRowChanged(gvAksPOTalepLines, null);
            gvAksPOTalepLines.DoubleClick += gvAksPOTalepLines_DoubleClick;
            gvAksPOTalepLines.SetFilterTypeToCheckedList("AksPOSipId","SipNo");

            SetButtonCaption();

            gvAksPOTalepLines.BestFitColumns();

            gvAksPOTalepLines.Columns["Notes"].Width = 100;
            gvAksPOTalepLines.Columns["SatinalmaNotu"].Width = 100;
            gvAksPOTalepLines.OptionsView.EnableAppearanceOddRow =gvAksPOTalepLines.OptionsView.EnableAppearanceEvenRow= false;

        }

        private void UndoTalep(object sender, EventArgs e)
        {
            int RowHandle = gvAksPOTalepLines.FocusedRowHandle;
            if (RowHandle > -1)
            {
                string Aksesuar = gvAksPOTalepLines.GetRowCellDisplayText(RowHandle, "AksesuarId").ToString();
                string TalepNo = gvAksPOTalepLines.GetRowCellValue(RowHandle, "SipNo").ToString();
                string QtyGelen = gvAksPOTalepLines.GetRowCellValue(RowHandle, "ToplamGelen").ToString();
                string QtyOrdered = gvAksPOTalepLines.GetRowCellValue(RowHandle, "QtyOrdered").ToString();
                if (XtraMessageBox.Show(" Talep No : " + TalepNo
                                    + "\n Aksesuar : " + Aksesuar
                                    + "\n Sipariş Adedi  : " + QtyOrdered
                                    + "\n Gelen Adet : " + QtyGelen
                                    + "\n\n Bu talep satırını GERİ ALMAK istediğinize emin misiniz?", "Kritik!", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                else
                {
                    gvAksPOTalepLines.SetRowCellValue(RowHandle, "stTalepId", 2);
                    bbiSave.PerformClick();
                    Yenile(); 
                }
            }
        }

        void gvAksPOTalepLines_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //e.HighPriority = true;
            int TerminiGecmis = gvAksPOTalepLines.GetRowCellValue(e.RowHandle, "TerminiGecmis").ToInt();
            int TedarikciTermini = gvAksPOTalepLines.GetRowCellValue(e.RowHandle, "TedarikciTerminiGecmis").ToInt();
            if (TerminiGecmis > 0)
            {
                //gvAksPOTalepLines.OptionsView.EnableAppearanceOddRow = gvAksPOTalepLines.OptionsView.EnableAppearanceEvenRow = false;
                e.Appearance.BackColor = Color.PaleGoldenrod;
            }
            //else
            //    gvAksPOTalepLines.OptionsView.EnableAppearanceOddRow = gvAksPOTalepLines.OptionsView.EnableAppearanceEvenRow = true;

            if (TerminiGecmis > 0 && TedarikciTermini > 0)
            {
                e.Appearance.BackColor = Color.LightPink;
            }
        }

        private void CancelTalep(object sender, EventArgs e)
        {
            string AksPOLinesIds = gvAksPOTalepLines.GetSelectedValues("AksPOLinesId");
            DataRow[] drSelected = gvAksPOTalepLines.DataTable().Select("AksPOLinesId in(" + AksPOLinesIds + ")");

            if (drSelected.Length > 0)
            {
                DataTable dtSelected = drSelected.CopyToDataTable();
                int FarklıPO = dtSelected.Select("AkspoId <> " + drSelected[0]["AkspoId"]).Length;

                if (FarklıPO > 0)
                {
                    ShowErrorMessage("Aynı anda iki farklı talebin satırlarını kapatamazsınız. Lütfen aynı talep numarasının satırlarını seçin.");
                    return;
                }
                string TalepNo = "Talep No : " + drSelected[0]["SipNo"].ToString(); ;

                string Uyari = "";
                foreach (DataRow dr in drSelected)
                {
                    string Brand = AppSession.Lookups.Brand.FindByBrandId(dr["BrandId"].ToInt()).Name;
                    string ProductCategory = AppSession.Lookups.ProductCategory.FindByProductCategoryId(dr["ProductCategoryId"].ToInt()).Name;
                    string StokKod = AppSession.Lookups.V_Aksesuar.FindByAksesuarId(dr["AksesuarId"].ToInt()).StokKod;
                    string SiparisAdedi = dr["QtyOrdered"].ToStr();
                    string GelenAdet = dr["ToplamGelen"].ToStr();

                    Uyari += "\nAksesuar : " + Brand + " " + ProductCategory + " " + StokKod + "・      Sipariş Adedi :" + SiparisAdedi + "・     Gelen Adet :" + GelenAdet;
                }

                string Soru = drSelected.Length > 1 ? "\n\nBu talep satırlarını İPTAL ETMEK istediğinize emin misiniz?" : "\n\nBu talep satırını İPTAL ETMEK istediğinize emin misiniz?";
                if (XtraMessageBox.Show(TalepNo + "\n" + Uyari + Soru, "Kritik!", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                else
                {
                    //foreach (DataRow dr in drSelected)
                    //{
                    //    dr["stTalepId"] = 4;
                    //}
                    if (gvAksPOTalepLines.Dataset().HasChanges())
                        bbiSave.PerformClick();

                    DataTable dtCikisBekleyen = ExecuteDataTable("CloseAksPOByAksPOLinesIds", AksPOLinesIds, WhoAmI, 4);
                    if (dtCikisBekleyen.Rows.Count > 0)
                    {
                        string HataMesaji = "Aşağıdaki aksesuarlar çıkış için bekleniyor. İptal işlemi gerçekleşmemiştir !\n\n";
                        foreach (DataRow dr in dtCikisBekleyen.Rows)
                        {
                            HataMesaji += dr["HataMesaji"].ToStr() + "\n";
                        }
                        ShowErrorMessage(HataMesaji);
                        return;
                    }

                    Yenile();
                }
            }           
        }
        private void TalebiKapat(object sender, EventArgs e)
        {            
            string AksPOLinesIds = gvAksPOTalepLines.GetSelectedValues("AksPOLinesId");
            DataRow[] drSelected = gvAksPOTalepLines.DataTable().Select("AksPOLinesId in(" + AksPOLinesIds + ")");

            if (drSelected.Length > 0)
            {
                DataTable dtSelected = drSelected.CopyToDataTable();
                int FarklıPO = dtSelected.Select("AkspoId <> " + drSelected[0]["AkspoId"]).Length;

                if (FarklıPO>0)
                {
                    ShowErrorMessage("Aynı anda iki farklı talebin satırlarını kapatamazsınız. Lütfen aynı talep numarasının satırlarını seçin.");
                    return;
                }
                string TalepNo = "Talep No : " + drSelected[0]["SipNo"].ToString(); ;

                string Uyari = "";
                foreach (DataRow dr in drSelected)
                {
                    string Brand = AppSession.Lookups.Brand.FindByBrandId(dr["BrandId"].ToInt()).Name;
                    string ProductCategory = AppSession.Lookups.ProductCategory.FindByProductCategoryId(dr["ProductCategoryId"].ToInt()).Name;
                    string StokKod = AppSession.Lookups.V_Aksesuar.FindByAksesuarId(dr["AksesuarId"].ToInt()).StokKod;
                    string SiparisAdedi = dr["QtyOrdered"].ToStr();
                    string GelenAdet = dr["ToplamGelen"].ToStr();

                    Uyari += "\nAksesuar : " + Brand + " " + ProductCategory + " " + StokKod + "・      Sipariş Adedi :" + SiparisAdedi + "・     Gelen Adet :" + GelenAdet;                                       
                }

                string Soru = drSelected.Length > 1 ? "\n\nBu talep satırlarını KAPATMAK istediğinize emin misiniz?" : "\n\nBu talep satırını KAPATMAK istediğinize emin misiniz?";
                if (XtraMessageBox.Show(TalepNo + "\n" + Uyari + Soru, "Kritik!", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                else
                {
                    //foreach (DataRow dr in drSelected)
                    //{
                    //    dr["stTalepId"] = 9;
                    //}
                    if (gvAksPOTalepLines.Dataset().HasChanges())
                        bbiSave.PerformClick();

                    DataTable dtCikisBekleyen= ExecuteDataTable("CloseAksPOByAksPOLinesIds", AksPOLinesIds, WhoAmI,9);
                    if (dtCikisBekleyen.Rows.Count>0)
                    {
                        string HataMesaji = "Aşağıdaki aksesuarlar çıkış için bekleniyor. Kapatma işlemi gerçekleşmemiştir !\n\n";
                        foreach (DataRow dr in dtCikisBekleyen.Rows)
                        {
                            HataMesaji += dr["HataMesaji"].ToStr()+"\n";
                        }
                        ShowErrorMessage(HataMesaji);
                        return;
                    }

                    Yenile();
                }
            }           
        }

        void gvAksPOTalepLines_DoubleClick(object sender, EventArgs e)
        {
            int handle = gvAksPOTalepLines.FocusedRowHandle;
            if (handle > -1)
            {
                int AksesuarId = intParse(gvAksPOTalepLines.GetRowCellValue(handle, "AksesuarId"));
                using (AksesuarHareketGC ah = new AksesuarHareketGC(AksesuarId, null, null))
                    ah.ShowDialog();
            }
        }

        void bbiFoy_ItemClick(object sender, ItemClickEventArgs e)
        {
            gvAksPOTalepLines.EndEditing();
            using (FoyIncele barkod = new FoyIncele("GetAksPOIdByBarkod"))
            {
                DialogResult dr = barkod.ShowDialog();
                if (dr == DialogResult.OK)
                    if (barkod.POId > 0)
                    {
                        int POId = barkod.POId;
                        if (POId > 1e8)
                            POId = POId % (int)1e8;

                        int len = gvAksPOTalepLines.DataTable().Select("AksPOId=" + POId.ToString()).Length;
                        if (len==0)
                        {
                            if (gvAksPOTalepLines.Dataset().HasChanges())                            
                                bbiSave.PerformClick();
                            
                            btnTemizle.PerformClick();
                            vgc.SetCellValue(vgc.Rows["Yil"], 0, 14);
                            Yenile();    
                        }
                        ExecuteNonQuery("UpdAksPOFoyTarih", POId); 
                        btnTemizle.PerformClick();
                        vgc.SetCellValue(vgc.Rows["Yil"], 0, 14);
                        Yenile(); 
                        gvAksPOTalepLines.ActiveFilterString = "AksPOId=" + POId.ToString();
                    }
            }
        }

        void gvAksPOTalepLines_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvAksPOTalepLines.FocusedRowHandle > -1)
            {
                int AksPOSipId = gvAksPOTalepLines.GetRowCellValue(gvAksPOTalepLines.FocusedRowHandle, "AksPOSipId").ToInt();
                int stTalepId = gvAksPOTalepLines.GetRowCellValue(gvAksPOTalepLines.FocusedRowHandle, "stTalepId").ToInt();
                if (AksPOSipId == 0 ||ytk(71))
                {
                    gvAksPOTalepLines.MakeReadOnly("QtyOrdered",  "VendorId", "AnlasilanFiyat","TedarikciTermini","SatinalmaNotu","AlternatifNot");
                }
                else if (AksPOSipId > 0 && stTalepId != 4 && stTalepId != 7 && stTalepId != 9)
                {
                    gvAksPOTalepLines.MakeReadOnly("QtyOrdered", "AnlasilanFiyat", "TedarikciTermini", "SatinalmaNotu");
                }
                else
                {
                    gvAksPOTalepLines.MakeReadOnly("AnlasilanFiyat", "TedarikciTermini", "SatinalmaNotu");
                }
            }
        }

        void gvHist_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName == "Sec")
                {
                    bool currentStatus = false;
                    if (e.CellValue != DBNull.Value)
                    {
                        currentStatus = (bool)e.CellValue;                        
                    }
                    gvAksPOTalepLines.SetRowCellValue(e.RowHandle, e.Column, !currentStatus);
                    SetButtonCaption();
                }

                if (e.Column.FieldName == "NumuneVarMi")
                {
                    bool currentStatus = false;
                    if (e.CellValue != DBNull.Value)
                    {
                        currentStatus = (bool)e.CellValue;
                    }
                    gvAksPOTalepLines.SetRowCellValue(e.RowHandle, e.Column, !currentStatus);
                    SetButtonCaption();
                }

            }
        }

        RepositoryItemButtonEdit emptyEditor;
        void gvHist_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
           
            int vendorId = gvAksPOTalepLines.GetRowCellValue(e.RowHandle, "VendorId").ToInt();
            int qtyOrdered = gvAksPOTalepLines.GetRowCellValue(e.RowHandle, "QtyOrdered").ToInt();
            int aksPOSipId = gvAksPOTalepLines.GetRowCellValue(e.RowHandle, "AksPOSipId").ToInt();

            if (e.Column.FieldName == "Sec" && (vendorId == 0 || qtyOrdered == 0 || aksPOSipId > 0))
                e.RepositoryItem = emptyEditor;           
        }

        void gvHist_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Name != "colSec")
            {
                gvAksPOTalepLines.EndEditing();
                foreach (DataRow drAks in gvAksPOTalepLines.DataTable().Select("Isnull(QtyOrdered,0)>0 and Isnull(VendorId,0)>0  and AksPOSipId is null and stTalepId = 1"))
                {
                    drAks["Sec"] = 1;
                }
                foreach (DataRow drAks in gvAksPOTalepLines.DataTable().Select("Isnull(QtyOrdered,0)=0 or Isnull(VendorId,0)=0  or AksPOSipId is not null and stTalepId = 1"))
                {
                    drAks["Sec"] = 0;
                }
            }
            SetButtonCaption();
        }

        void btnSipOlustur_ItemClick(object sender, ItemClickEventArgs e)
        {
            //bbiSave.PerformClick();

            DataTable dt = gvAksPOTalepLines.DataTable().Select("Isnull(Sec,0)=1").CopyToDataTable();
            using(AksPOSipOlustur aks = new AksPOSipOlustur(dt))
                if (aks.ShowDialog() == DialogResult.OK)
                {
                    DataTable dtAks = aks.DtAksPOSip;
                    foreach (DataRow drAks in gvAksPOTalepLines.DataTable().Select("Isnull(Sec,0)=1"))
                    {
                        int AksPOSipId = dtAks.Select("AksPOLinesId=" + drAks["AksPOLinesId"])[0]["AksPOSipId"].ToInt();
                        drAks["AksPOSipId"] = AksPOSipId;
                    }
                    bbiSave.PerformClick();
                    Yenile();

                    //var qrAksPOSipGroup = from item in aks.DtAksPOSip.AsEnumerable()
                    //                      group item by new { AksPOSipId = item.Field<int>("AksPOSipId"), SipNo = item.Field<string>("SipNo") }
                    //                          into grp
                    //                          select new
                    //                          {
                    //                              AksPOSipId = grp.Key.AksPOSipId,
                    //                              SipNo = grp.Key.SipNo
                    //                          };

                    var qrAksPOSipGroup = from item in aks.DtAksPOSip.AsEnumerable()
                                          group item by new { AksPOSipId = item.Field<int>("AksPOSipId") }
                                              into grp
                                              select new
                                              {
                                                  AksPOSipId = grp.Key.AksPOSipId
                                              };

                    foreach (var item in qrAksPOSipGroup)
                    {
                        int aksposipId = item.AksPOSipId;
                        Rapor.Aksesuar.xrAksesuarSiparisFoy foy = new Rapor.Aksesuar.xrAksesuarSiparisFoy(aksposipId.ToString(), "GetAksesuarSiparisFoyuBySipId", "AksPOSipId");
                        MemoryStream ms = new MemoryStream();
                        foy.ExportToXls(ms);
                        byte[] foyPdf = ms.ToArray();

                        if (isProduction)
                            ExecuteNonQuery("SendMailforAksPO", aksposipId, foyPdf);
                    }
                }
        }

        void SetButtonCaption()
        {
            gvAksPOTalepLines.EndEditing();
            
            int rowCount = gvAksPOTalepLines.DataTable().Select("Isnull(Sec,0)=1").Length;
            if (rowCount == 0)
            {
                btnSipOlustur.Visibility = BarItemVisibility.Never;
            }
            else
            {
                btnSipOlustur.Visibility = BarItemVisibility.Always;
                btnSipOlustur.Caption = rowCount + " satır için &Sipariş Oluştur";
            }
        }

        void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //int BosNumuneVarmi = gvAksPOTalepLines.DataTable().Select("NumuneVarmi is null").Length;

            //if (BosNumuneVarmi > 0)
            //{
            //    ShowErrorMessage("Lütfen NumuneVarmi alanını boş bırakmayın");
            //    return;
            //}
            gvAksPOTalepLines.EndEditing();
            gvAksPOTalepLines.Update("AksPOLinesId, QtyOrdered, VendorId,NumuneVarMi, AnlasilanFiyat,AksPOSipId,TedarikciTermini,SatinalmaNotu,stTalepId,AlternatifNot", "AksPOLines");            
        }


        void gvHist_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "QtyNeeded" ||
                e.Column.FieldName == "stTalepId" ||
                e.Column.FieldName == "StokKod"
                )
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);            
        }


        private void CreateVerticalGrid()
        {
            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("TumTalepler", typeof(bool)),
                AddItem("Yil", typeof(int)),
                AddItem("SipNo", typeof(string)),
                AddItem("ProductCategoryId", typeof(int), RI(Rp.ProductCategory)),
                AddItem("AksesuarId", typeof(int), RI(Rp.V_Aksesuar)),
                AddItem("BrandId", typeof(int), RI(Rp.Brand)),
                AddItem("Tedarikci", typeof(int), RI(Rp.Vendor)),
                AddItem("Numune", typeof(string)),
                //AddItem("Durumu", typeof(string)),
                AddItem("Aciklama", typeof(string)),
                AddItem("TalepEdenId", typeof(int), RI(Rp.V_User))
                //AddItem("AksTuru", typeof(string)),
                //AddItem("Musteri", typeof(string)),
                //AddItem("Uretici", typeof(string)),
                //AddItem("Planlamaci", typeof(string)),
                //AddItem("SiparisinTamami", typeof(bool))
            });

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            vgc.SetCellValue(vgc.Rows["Yil"], 0, 14);
            //vgc.FocusedRow = vgc.Rows["ProductCategoryId"];
            vgc.BestFit();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            Yenile();            
        }

        private void Yenile()
        {
            int focusedRow = gvAksPOTalepLines.FocusedRowHandle;
            gvAksPOTalepLines.RefreshDataSource(true, "GetAksPOSubmitted",
                boolParse(GetVal("TumTalepler")),
                intParse(GetVal("Yil")),
                strParse(GetVal("SipNo")),
                intParse(GetVal("ProductCategoryId")),
                intParse(GetVal("AksesuarId")),
                intParse(GetVal("BrandId")),
                intParse(GetVal("Tedarikci")),
                strParse(GetVal("Numune")),
                //strParse(GetVal("Durumu")),
                strParse(GetVal("Aciklama")),
                intParse(GetVal("TalepEdenId")),
                WhoAmI
                //strParse(GetVal("AksTuru")),
                //strParse(GetVal("Musteri")),
                //strParse(GetVal("Uretici")),
                //strParse(GetVal("Planlamaci")),
                //boolParse(GetVal("SiparisinTamami"))
                );


            if (focusedRow + 1 >= gvAksPOTalepLines.RowCount)
            {
                gvAksPOTalepLines.FocusedRowHandle = gvAksPOTalepLines.RowCount - 2;
            }
            else
            {
                gvAksPOTalepLines.FocusedRowHandle = focusedRow;
            }

            SetButtonCaption();
            gvAksPOTalepLines.BestFitColumns();
            gvAksPOTalepLines.Columns["SatinalmaNotu"].Width = 100;
            gvAksPOTalepLines.Columns["Notes"].Width = 100;


            Notify("Done.");
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }
    }
}